package com.hanscan.persistence.controllers;


import com.hanscan.persistence.EntityManagerSingleton;

import com.hanscan.persistence.controllers.exceptions.NonexistentEntityException;
import com.hanscan.persistence.entities.Event;
import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;


public class EventJpaController implements Serializable
{

    public EventJpaController()
    {
    }

    public EntityManager getEntityManager()
    {
        return EntityManagerSingleton.getInstance();
    }

    public void create(Event event) throws Exception
    {
        //TODO: comprobaciones
        EntityManager em = null;
        try
        {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(event);
            em.getTransaction().commit();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public void edit(Event event) throws NonexistentEntityException, Exception
    {
        //TODO: comprobaciones
        EntityManager em = null;
        try
        {
            em = getEntityManager();
            em.getTransaction().begin();
            event = em.merge(event);
            em.getTransaction().commit();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public void destroy(Event event) throws Exception
    {
        //TODO: comprobaciones
        //TODO: si se borra usuario, se borran tb sus localizaciones, a no ser q el borrado sea logico
        EntityManager em = null;
        try
        {
            em = getEntityManager();
            em.getTransaction().begin();
            em.remove(event);
            em.getTransaction().commit();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public List<Event> findEventEntities()
    {
        return findEventEntities(true, -1, -1);
    }

    public List<Event> findEventEntities(int maxResults, int firstResult)
    {
        return findEventEntities(false, maxResults, firstResult);
    }

    private List<Event> findEventEntities(boolean all, int maxResults,
            int firstResult)
    {
        EntityManager em = getEntityManager();
        try
        {
            Query q = em.createQuery("select object(o) from Event as o");
            if (!all)
            {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        }
        finally
        {
            em.close();
        }
    }

    public Event findEvent(Long id)
    {
        EntityManager em = getEntityManager();
        try
        {
            return em.find(Event.class, id);
        }
        finally
        {
            em.close();
        }
    }

    public int getEventCount()
    {
        EntityManager em = getEntityManager();
        try
        {
            Query q = em.createQuery("select count(o) from Event as o");
            return ((Long) q.getSingleResult()).intValue();
        }
        finally
        {
            em.close();
        }
    }

    public List<Event> getEventsThisMonth(long idUser)
    {
        Calendar c = Calendar.getInstance();
        int m = c.get(Calendar.MONTH);
        m++;
        EntityManager em = getEntityManager();
        em.clear();

        Calendar ci = Calendar.getInstance();

        ci.set(Calendar.MONTH, m);
        ci.set(Calendar.DAY_OF_MONTH, 1);
        ci.set(Calendar.HOUR_OF_DAY, 00);
        ci.set(Calendar.MINUTE, 00);
        ci.set(Calendar.SECOND, 00);

        Calendar cf = Calendar.getInstance();

        cf.set(Calendar.MONTH, m+1);
        cf.set(Calendar.DAY_OF_MONTH, 1);
        cf.set(Calendar.HOUR_OF_DAY, 00);
        cf.set(Calendar.MINUTE, 00);
        cf.set(Calendar.SECOND, 00);


        Date datei = new Date();
        datei.setTime(ci.getTimeInMillis());
        Date datef = new Date();
        datef.setTime(cf.getTimeInMillis());

        try
        {
            Query query = em.createQuery("select s from Event as s where" +
             "(s.dateTime >= '" + datei + "' and s.dateTime <= '" +
             datef + "') and s.user.userId = " + idUser + " order by s.dateTime");
            return query.getResultList();
        }
        finally
        {
            em.close();
        }
    }

    public List<Event> findSigningByClientAndDay(long idUser, Date date)
    {
        Calendar c = Calendar.getInstance();
        int m = c.get(Calendar.MONTH);
        m++;
        EntityManager em = getEntityManager();
        em.clear();

        Calendar ci = Calendar.getInstance();

        ci.set(Calendar.MONTH, m);
        ci.set(Calendar.DAY_OF_MONTH, 1);
        ci.set(Calendar.HOUR_OF_DAY, 00);
        ci.set(Calendar.MINUTE, 00);
        ci.set(Calendar.SECOND, 00);

        Calendar cf = Calendar.getInstance();

        cf.set(Calendar.MONTH, m+1);
        cf.set(Calendar.DAY_OF_MONTH, 1);
        cf.set(Calendar.HOUR_OF_DAY, 00);
        cf.set(Calendar.MINUTE, 00);
        cf.set(Calendar.SECOND, 00);


        Date datei = new Date();
        datei.setTime(ci.getTimeInMillis());
        Date datef = new Date();
        datef.setTime(cf.getTimeInMillis());

        try
        {
            Query query = em.createQuery("select s from Event as s where" +
             "(s.dateTime >= '" + datei + "' and s.dateTime <= '" +
             datef + "') and s.user.userId = " + idUser + " order by s.dateTime");
            return query.getResultList();
        }
        finally
        {
            em.close();
        }
    }

}
